package com.project.decoration.controller.style;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.decoration.common.enums.BusinessType;
import com.project.decoration.common.enums.ResultCode;
import com.project.decoration.domain.DecorationCase;
import com.project.decoration.domain.DecorationPrice;
import com.project.decoration.domain.DecorationStyle;
import com.project.decoration.domain.Result;
import com.project.decoration.service.DecorationCaseService;
import com.project.decoration.service.DecorationPriceService;
import com.project.decoration.service.DecorationStyleService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 超级管理员
 * @version 1.0
 * @description: 装修风格controller
 * @date 2025/02/13 09:02
 */
@Controller
@ResponseBody
@RequestMapping("style")
public class DecorationStyleController {

    @Autowired
    private DecorationStyleService decorationStyleService;
    @Autowired
    private DecorationPriceService decorationPriceService;
    @Autowired
    private DecorationCaseService decorationCaseService;

    /** 分页获取装修风格 */
    @PostMapping("getDecorationStylePage")
    public Result getDecorationStylePage(@RequestBody DecorationStyle decorationStyle) {
        Page<DecorationStyle> page = new Page<>(decorationStyle.getPageNumber(),decorationStyle.getPageSize());
        QueryWrapper<DecorationStyle> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .like(StringUtils.isNotBlank(decorationStyle.getName()),DecorationStyle::getName,decorationStyle.getName());
        Page<DecorationStyle> decorationStylePage = decorationStyleService.page(page, queryWrapper);
        return Result.success(decorationStylePage);
    }

    @GetMapping("getDecorationStyleList")
    public Result getDecorationStyleList() {
        List<DecorationStyle> styleList = decorationStyleService.list();
        return Result.success(styleList);
    }

    /** 根据id获取装修风格 */
    @GetMapping("getDecorationStyleById")
    public Result getDecorationStyleById(@RequestParam("id")String id) {
        DecorationStyle decorationStyle = decorationStyleService.getById(id);
        return Result.success(decorationStyle);
    }

    /** 保存装修风格 */
    @PostMapping("saveDecorationStyle")
    public Result saveDecorationStyle(@RequestBody DecorationStyle decorationStyle) {
        boolean save = decorationStyleService.save(decorationStyle);
        if (save) {
            return Result.success();
        } else {
            return Result.fail(ResultCode.COMMON_DATA_OPTION_ERROR.getMessage());
        }
    }

    /** 编辑装修风格 */
    @PostMapping("editDecorationStyle")
    public Result editDecorationStyle(@RequestBody DecorationStyle decorationStyle) {
        boolean save = decorationStyleService.updateById(decorationStyle);
        if (save) {
            return Result.success();
        } else {
            return Result.fail(ResultCode.COMMON_DATA_OPTION_ERROR.getMessage());
        }
    }

    /** 删除装修风格 */
    @GetMapping("removeDecorationStyle")
    public Result removeDecorationStyle(@RequestParam("ids")String ids) {
        if (StringUtils.isNotBlank(ids)) {
            String[] asList = ids.split(",");
            for (String id : asList) {
                DecorationStyle style = decorationStyleService.getById(id);
                QueryWrapper<DecorationCase> queryWrapper = new QueryWrapper<>();
                queryWrapper.lambda().eq(DecorationCase::getStyle,style.getName());
                int count = decorationCaseService.count(queryWrapper);
                if (count > 0) {
                    return Result.fail("风格下存在案例，无法删除");
                }
                QueryWrapper<DecorationPrice> wrapper = new QueryWrapper<>();
                wrapper.lambda().like(DecorationPrice::getStyle,style.getName());
                int counted = decorationPriceService.count(wrapper);
                if (counted > 0) {
                    return Result.fail("风格下存在套餐，无法删除");
                }
                decorationStyleService.removeById(id);
            }
            return Result.success();
        } else {
            return Result.fail("装修风格id不能为空！");
        }
    }

}
